//
//  ViewController3.m
//  Wedding fantastic
//
//  Created by ZLM on 15/6/29.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "ShangJiaViewController.h"

@interface ShangJiaViewController ()

@end

@implementation ShangJiaViewController
-(void)dealloc
{
    [_myButtonName release];
    [_aCell release];
    [_strUrl release];
    [super dealloc];

}
-(void)viewWillDisappear:(BOOL)animated{

    self.navigationController.navigationBar.translucent = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.view.backgroundColor = [UIColor clearColor];
    
    self.myPage = 1;
    
    // 回到顶部button
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(300 * kWidth / 375, kHeight -  130 * kHeight / 667, 35 * kWidth / 375, 38 * kHeight / 667);
    [button setBackgroundImage:[UIImage imageNamed:@"backTop"] forState:UIControlStateNormal];
    button.alpha = 0.6;
    [self.view addSubview:button];
    [button addTarget:self action:@selector(backTop:) forControlEvents:UIControlEventTouchUpInside];
    //上下拉刷新
    
    //网络请求
    [self addHeader];
    [self addFooter];
    
//    [self setupRefresh];
}
// 回到顶部
- (void)backTop:(UIButton *)button
{
    [self.myTableView setContentOffset:CGPointZero animated:YES];
}
//获取按钮名称，并调用网络获取数据方法
-(void)setMyButtonName:(NSString *)myButtonName{
    if (_myButtonName != myButtonName) {
        [_myButtonName release];
        _myButtonName = [myButtonName retain];
    }
    self.navigationItem.title = self.myButtonName;
//    让大风车转起来
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self.HUD setDimBackground:YES];
    self.HUD.labelText = @"Loading";
    [self.HUD show:YES];
    
    [self getData];
}
//获取网络数据
-(void)getData{
    NSString *urlString = @"";
    if ([self.myButtonName isEqualToString:@"婚礼策划"]) {
        
        urlString = [((NSMutableDictionary *)[((NSMutableArray *)[self.allArr objectAtIndex:0]) objectAtIndex:2]) objectForKey:@"商家"];
        NSLog(@"2222222222222%@",urlString);
        self.strUrl = urlString;
    }
    if ([self.myButtonName isEqualToString:@"婚纱摄影"]) {
        urlString = [((NSMutableDictionary *)[((NSMutableArray *)[self.allArr objectAtIndex:1]) objectAtIndex:2]) objectForKey:@"商家"];
        self.strUrl = urlString;
    }
    if ([self.myButtonName isEqualToString:@"摄影师"]) {
        urlString = [((NSMutableDictionary *)[((NSMutableArray *)[self.allArr objectAtIndex:2]) objectAtIndex:2]) objectForKey:@"商家"];
        self.strUrl = urlString;
    }
    if ([self.myButtonName isEqualToString:@"摄像师"]) {
        urlString = [((NSMutableDictionary *)[((NSMutableArray *)[self.allArr objectAtIndex:3]) objectAtIndex:2]) objectForKey:@"商家"];
        self.strUrl = urlString;
    }
    if ([self.myButtonName isEqualToString:@"化妆师"]) {
        urlString = [((NSMutableDictionary *)[((NSMutableArray *)[self.allArr objectAtIndex:4]) objectAtIndex:2]) objectForKey:@"商家"];
        self.strUrl = urlString;
    }
    if ([self.myButtonName isEqualToString:@"主持人"]) {
        urlString = [((NSMutableDictionary *)[((NSMutableArray *)[self.allArr objectAtIndex:5]) objectAtIndex:2]) objectForKey:@"商家"];
        self.strUrl = urlString;
    }
    
//   解析网络数据
    [NetworkTool get:urlString body:BodyHTTP httpResult:JSON success:^(id result) {
        
//       给商家model赋值
        NSMutableArray *array = result[@"merchants"];
        
        self.shangJiaModelArray = [NSMutableArray array];
        for (NSMutableDictionary *tempDic in array) {
            shangJiaModel *mod = [[shangJiaModel alloc] init];
            [mod setValuesForKeysWithDictionary:tempDic];
            [self.shangJiaModelArray addObject:mod];
            [mod release];
        }
//        隐藏大风车
        [self.HUD hide:YES];
        [self.myTableView reloadData];
    }
//     网络数据获取失败
    failure:^(NSError *error) {
        UIAlertView *alertV = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"网络不给力啊" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertV show];
        
    }];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
//商家model数组元素个数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.shangJiaModelArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *reuseIdentifer = @"reuse";
    self.aCell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifer];
    if (self.aCell == nil) {
        self.aCell = [[ShangJiaTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifer];
    }

    shangJiaModel *mod = [self.shangJiaModelArray objectAtIndex:indexPath.row];
// 商家图片
    [self.aCell.imageV sd_setImageWithURL:[NSURL URLWithString:mod.logo_path] placeholderImage:[UIImage imageNamed:@"placeHoderImage"]];
    self.aCell.picUrl = mod.logo_path;
//   商家名称
    self.aCell.nameLabel.text = mod.name;
    self.aCell.nameLabel.font = [UIFont systemFontOfSize:14];
// 商家粉丝
    self.aCell.fansLabel.text = [NSString stringWithFormat:@"❤️ 关注 :%@",mod.fans_count];
    //3D效果
    self.aCell.layer.transform = CATransform3DMakeScale(0.3, 0.3, 0.1);
    [UIView animateWithDuration:0.5 animations:^{
        
        self.aCell.layer.transform = CATransform3DMakeScale(1, 1, 0.1);
    }];
    return self.aCell;
}
//行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return  96;
}
////section标题名称
//-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
//    
//    return self.myButtonName;
//    
//}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
 /*   [super tableView:tableView didSelectRowAtIndexPath:indexPath];
    //   打开数据库
    [[ShangJiaDataBaseHandle shareDataBase]openDB];
    //    创建表
    [[ShangJiaDataBaseHandle shareDataBase]createTable];
    //    获取当前内容
    shangJiaModel *mod = [[shangJiaModel alloc]init];
    mod = [self.shangJiaModelArray objectAtIndex:indexPath.row];
    NSLog(@"!!!!!!!!!!!%@",mod);
//    mod.name = [mod.merchant objectForKey:@"name"];
    //   内容 插入表中
    [[ShangJiaDataBaseHandle shareDataBase]insert:mod];

*/
    //商家详情
    SellDetailViewController *sellVC = [SellDetailViewController alloc];
    shangJiaModel *mod = [self.shangJiaModelArray objectAtIndex:indexPath.row];
    sellVC.sendModel = [[RecommendModel alloc]init];
    sellVC.sendModel.bId = mod.id;
    sellVC.sendModel.name = mod.name;
    sellVC.sendModel.logo_path = mod.logo_path;
    sellVC.sendModel.address = mod.address;
    sellVC.sendModel.cover_path = mod.cover_path;
    [self.navigationController pushViewController:sellVC animated:YES];
    
}
//
- (void)addHeader
{
    __block ShangJiaViewController *shangjiaVC = self;
    
    // 添加下拉刷新头部控件
    [self.myTableView addHeaderWithCallback:^{
        // 进入刷新状态就会回调这个Block
        shangjiaVC.myPage = 0;
        shangjiaVC.Loading = NO;//标记为下拉操作
        [self reCreateData];//重新请求数据
        
    }];
#pragma mark 自动刷新(一进入程序就下拉刷新)
    
}

#pragma mark --上拉加载更多
- (void)addFooter
{
    __block ShangJiaViewController *shangjiaVC = self;
    // 添加上拉刷新尾部控件
    [self.myTableView addFooterWithCallback:^{
        // 进入刷新状态就会回调这个Block
        shangjiaVC.Loading = YES;//标记为上拉操作
        [shangjiaVC reCreateData];//请求更多数据
        
    }];
}
-(void)reCreateData

{
    
    self.myPage++;
    NSString *strURL = [self.strUrl stringByReplacingOccurrencesOfString:@"page=1" withString:[NSString stringWithFormat:@"page=%ld",(long)self.myPage]];
        //   解析网络数据
        [NetworkTool get:strURL body:BodyHTTP httpResult:JSON success:^(id result) {
    
            //       给商家model赋值
            NSMutableArray *array = result[@"merchants"];
    
            //        self.shangJiaModelArray = [NSMutableArray array];
            for (NSMutableDictionary *tempDic in array) {
                shangJiaModel *mod = [[shangJiaModel alloc] init];
                [mod setValuesForKeysWithDictionary:tempDic];
                [self.shangJiaModelArray addObject:mod];
                [mod release];
            }
            //        隐藏大风车
            [self.HUD hide:YES];
            [self.myTableView reloadData];
        }
         //     网络数据获取失败
                 failure:^(NSError *error) {
                     UIAlertView *alertV = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"网络不给力啊" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                     [alertV show];
    
                 }];
    
            [self.myTableView headerEndRefreshing];
            [self.myTableView footerEndRefreshing];
            [self.myTableView reloadData];


    
}

//// 刷新
//- (void)setupRefresh
//{
//    // 下拉刷新
//    [self.myTableView addHeaderWithTarget:self action:@selector(headerRereshing)];
//    [self.myTableView headerBeginRefreshing];
//    
//    // 上拉加载
//    [self.myTableView addFooterWithTarget:self action:@selector(footerRereshing)];
//    //    [self.myTableView footerBeginRefreshing];
//}
//// 下拉
//- (void)headerRereshing
//{
//    //    [self.goodsCollectionView reloadData];
//    // 刷新结束
//    [self.myTableView headerEndRefreshing];
//}
//// 上拉
//- (void)footerRereshing
//{
//    self.myPage++;
//    NSString *strURL = [self.strUrl stringByReplacingOccurrencesOfString:@"page=1" withString:[NSString stringWithFormat:@"page=%ld",(long)self.myPage]];
//    //   解析网络数据
//    [NetworkTool get:strURL body:BodyHTTP httpResult:JSON success:^(id result) {
//        
//        //       给商家model赋值
//        NSMutableArray *array = result[@"merchants"];
//        
//        //        self.shangJiaModelArray = [NSMutableArray array];
//        for (NSMutableDictionary *tempDic in array) {
//            shangJiaModel *mod = [[shangJiaModel alloc] init];
//            [mod setValuesForKeysWithDictionary:tempDic];
//            [self.shangJiaModelArray addObject:mod];
//            [mod release];
//        }
//        //        隐藏大风车
//        [self.HUD hide:YES];
//        [self.myTableView reloadData];
//    }
//     //     网络数据获取失败
//             failure:^(NSError *error) {
//                 UIAlertView *alertV = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"网络不给力啊" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
//                 [alertV show];
//                 
//             }];
//    [self.myTableView reloadData];
//    [self.myTableView footerEndRefreshing];
//}

//// 刷新
//- (void)setupRefresh
//{
//    // 下拉刷新
//    [self.myTableView addHeaderWithTarget:self action:@selector(headerRereshing)];
//    [self.myTableView headerBeginRefreshing];
//    
//    // 上拉加载
//    [self.myTableView addFooterWithTarget:self action:@selector(footerRereshing)];
//    //    [self.myTableView footerBeginRefreshing];
//}
//// 下拉
//- (void)headerRereshing
//{
//    //    [self.goodsCollectionView reloadData];
//    // 刷新结束
//    [self.myTableView headerEndRefreshing];
//}
//// 上拉
//- (void)footerRereshing
//{
//    self.myPage++;
//    NSString *strURL = [self.strUrl stringByReplacingOccurrencesOfString:@"page=1" withString:[NSString stringWithFormat:@"page=%ld",self.myPage]];
//    //   解析网络数据
//    [NetworkTool get:strURL body:BodyHTTP httpResult:JSON success:^(id result) {
//        
//        //       给商家model赋值
//        NSMutableArray *array = result[@"merchants"];
//        
//        //        self.shangJiaModelArray = [NSMutableArray array];
//        for (NSMutableDictionary *tempDic in array) {
//            shangJiaModel *mod = [[shangJiaModel alloc] init];
//            [mod setValuesForKeysWithDictionary:tempDic];
//            [self.shangJiaModelArray addObject:mod];
//            [mod release];
//        }
//        //        隐藏大风车
//        [self.HUD hide:YES];
//        [self.myTableView reloadData];
//    }
//     //     网络数据获取失败
//             failure:^(NSError *error) {
//                 UIAlertView *alertV = [[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"网络不给力啊" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
//                 [alertV show];
//                 
//             }];
//    [self.myTableView reloadData];
//    [self.myTableView footerEndRefreshing];
//}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
